var express = require('express');
var router = express.Router();
var async = require('async');
var BaseData = require('../.././config/db');
db = new BaseData();
db.connection.connect();

/* GET home page. */
router.get('/', function(req, res, next) {

})

//获取用户信息
router.post('/user', function(req, res, next) {
	var limit = parseInt(req.body.limit),
		page = parseInt(req.body.page - 1) * limit,
		numlimit = 0;
	//获取用户总数
	db.connection.query('select COUNT(*) from userinfo', function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "系统出现错误,请联系管理员"
			})
		} else {
			numlimit = rows[0]['COUNT(*)'];
			//分页查询
			db.connection.query('SELECT `User_id`,`User`,`Name`,`Sex`,`UserIntro`,`SubNumber`,`Email`,`CentNumber`,`ColleNumber`,`InforNumber`,`CentStop`,`SubStop`,`LogStop`,`WoB`,`RegTime`,`RegIp`,`LogTime`,`LogIp` FROM `userinfo` limit ?,?', [page, limit], function(err, rows) {
				if(err) {
					res.json({
						"code": "3002",
						"msg": "系统出现错误,请联系管理员"
					})
				} else {
					res.json({
						"code": "0",
						"msg": "success",
						"count": numlimit,
						"data": rows
					})
				}
			})
		}
	})
})

//获取内容
router.post('/info', function(req, res, next) {
	var limit = parseInt(req.body.limit),
		page = parseInt(req.body.page - 1) * limit,
		numlimit = 0;

	//分页查询
	db.connection.query('select COUNT(*) from subinfo', function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "系统出现错误,请联系管理员"
			})
		} else {
			numlimit = rows[0]['COUNT(*)'];
			//分页查询
			db.connection.query('select sub.Sub_id,sub.User_id,sub.Content,sub.ContentType,sub.ContentAnnex,sub.SubCent,sub.SubColle,sub.UpNumber,sub.BuryNumber,sub.SubTime,sub.Status,user.Name from subinfo sub inner join userinfo user on sub.User_id=user.User_id limit ?,?', [page, limit], function(err, rows) {
				if(err) {
					res.json({
						"code": "3002",
						"msg": "系统出现错误,请联系管理员"
					})
				} else {
					console.log(rows)
					res.json({
						"code": "0",
						"msg": "success",
						"count": numlimit,
						"data": rows
					})
				}
			})
		}
	})
})

//审核通过
router.post('/pass', function(req, res, next) {
	var id = req.body.id;
	//修改状态
	db.connection.query('UPDATE `subinfo` SET `Status`= 2 where `Sub_id` = ?', [id], function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "审核出现错误，code：2"
			})
		} else {
			res.json({
				"code": "0",
				"msg": "success"
			})
		}
	})
})
//审核未通过
router.post('/filter', function(req, res, next) {
	var id = req.body.id;
	//修改状态
	db.connection.query('UPDATE `subinfo` SET `Status`= 1 where `Sub_id` = ?', [id], function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "审核出现错误，code：1"
			})
		} else {
			res.json({
				"code": "0",
				"msg": "success"
			})
		}
	})
})
//审核删除
router.post('/del', function(req, res, next) {
	var id = req.body.id;
	//修改状态
	db.connection.query('DELETE FROM `subinfo` WHERE `Sub_id`=?', [id], function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "删除失败"
			})
		} else {
			res.json({
				"code": "0",
				"msg": "success"
			})
		}
	})
})

//用户编辑
router.post('/useredit', function(req, res, next) {
	var id = db.DefenseXSS(req.body.User_id),
		User = db.DefenseXSS(req.body.User),
		Sex = db.DefenseXSS(req.body.Sex),
		UserIntro = db.DefenseXSS(req.body.UserIntro),
		Email = db.DefenseXSS(req.body.Email),
		CentStop = db.DefenseXSS(req.body.CentStop),
		SubStop = db.DefenseXSS(req.body.SubStop),
		LogStop = db.DefenseXSS(req.body.LogStop),
		WoB = db.DefenseXSS(req.body.WoB),
		msg = "";
	//修改状态
	if(id == null || User == null || Sex == null || UserIntro == null || Email == null || CentStop == null || SubStop == null || LogStop == null || WoB == null) {
		msg = "内容不能为空";
	}
	if(msg) {
		res.json({
			code: "3001",
			msg: msg
		})
		return false;
	}
	db.connection.query('UPDATE `userinfo` SET `User`= ?,`Sex`=?,`UserIntro`=?,`Email`=?,`CentStop`=?,`SubStop`=?,`LogStop`=?,`WoB`=? where `User_id` = ?', [User, Sex, UserIntro, Email, CentStop, SubStop, LogStop, WoB, id], function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "保存失败"
			})
		} else {
			res.json({
				"code": "0",
				"msg": "保存 "
			})
		}
	})
})


module.exports = router;